//
//  MyPhotoCollectionViewCell.m
//  Day5_图片浏览
//
//  Created by apple on 16/5/17.
//  Copyright © 2016年 ljp. All rights reserved.
//

#import "MyPhotoCollectionViewCell.h"
#import "UIImageView+WebCache.h"

@interface MyPhotoCollectionViewCell ()<UIScrollViewDelegate>
{
    UIScrollView *_scrollView;
    UIImageView *_imageView;
    NSTimer *tapTimer;
}
@end

@implementation MyPhotoCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

        [self createSubviews];
    }
    return self;
}

- (void)createSubviews {

    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self.contentView addSubview:_scrollView];

    _imageView = [[UIImageView alloc] initWithFrame:_scrollView.bounds];
    [_scrollView addSubview:_imageView];

    //设置滑动范围
    _scrollView.contentSize = _imageView.frame.size;
    //设置拉伸模式
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    //设置多点触碰
    _scrollView.multipleTouchEnabled = YES;
    //设置缩放比例
    _scrollView.maximumZoomScale = 2;
    _scrollView.minimumZoomScale = 0.5;
    _scrollView.delegate = self;

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
    [_scrollView addGestureRecognizer:doubleTap];
    //设置点击次数
    doubleTap.numberOfTapsRequired = 2;
    //设置触碰点数
    doubleTap.numberOfTouchesRequired = 1;

    UITapGestureRecognizer *oneTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTapAction:)];
    [_scrollView addGestureRecognizer:oneTap];
    //设置点击次数
    oneTap.numberOfTapsRequired = 1;
    //设置触碰点数
    oneTap.numberOfTouchesRequired = 1;

}
- (void)oneTapAction:(UITapGestureRecognizer *)tap{

    tapTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(tapTimerAction) userInfo:nil repeats:NO];

}
- (void)tapTimerAction {
    //向viewcontroller发出隐藏导航栏的通知
    [[NSNotificationCenter defaultCenter] postNotificationName:KHideNavigationBar object:nil];
}
- (void)doubleTapAction:(UITapGestureRecognizer *)tap{

    [tapTimer invalidate];
    if (_scrollView.zoomScale > 1) {
        [_scrollView setZoomScale:1 animated:YES];
    }else {
        [_scrollView setZoomScale:2 animated:YES];
    }

}
//重写设置器方法
- (void)setUrl:(NSURL *)url {

    [_imageView sd_setImageWithURL:url];
}

- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    return _imageView;
}

-(void)backToOneScale {

    _scrollView.zoomScale = 1;
}

@end
